package com.dbuil.bleshoes.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.location.Location;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dbuil.bleshoes.R;
import com.dbuil.bleshoes.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;


import butterknife.BindView;
import butterknife.OnClick;

public class RunActivity extends BaseActivity {
    private static final boolean ISSTARTUP = true;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.amap)
    MapView mMapView;

    AMap aMap;
    private MarkerOptions markerOption;
    private List<LatLng> latList;

    LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();//存放所有点的经纬度



    @Override
    public int getLayoutId() {
        return R.layout.activity_run;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMapView.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//保持屏幕常亮
        tvTopTitle.setText("设备分布图");
        if (aMap == null) {
            aMap = mMapView.getMap();
        }

        MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.radiusFillColor(Color.parseColor("#331DC7BC"));//精度圈颜色
        myLocationStyle.strokeColor(Color.parseColor("#331DC7BC"));//精度圈边框颜色
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。

        //设置缩放比例
        aMap.moveCamera(CameraUpdateFactory.zoomTo(16));
        //是否显示缩放按钮
        aMap.getUiSettings().setZoomControlsEnabled(false);
        //aMap.getUiSettings().setZoomGesturesEnabled(false);
        //是否显示比例尺
        aMap.getUiSettings().setScaleControlsEnabled(false);
        //是否显示指南针
        aMap.getUiSettings().setCompassEnabled(false);
        // 设置默认定位按钮是否显示，非必需设置。
        aMap.getUiSettings().setMyLocationButtonEnabled(false);

        //设置点击事件
        aMap.setOnMarkerClickListener(markerClickListener);
        aMap.setOnMyLocationChangeListener(mlocationChangeListener);


        //showMarkers();
    }

    private void showMarkers() {
        latList = new ArrayList<>();
        latList.add(new LatLng(34.782529, 113.610593));
        latList.add(new LatLng(34.784531, 113.608635));
        latList.add(new LatLng(34.783122, 113.607018));
        latList.add(new LatLng(34.781092, 113.608168));
        latList.add(new LatLng(34.780217, 113.607755));
        latList.add(new LatLng(34.778838, 113.610342));


        for (int i = 0; i < latList.size(); i++) {
            LatLng latLng = latList.get(i);
            addMarkersToMap(latLng,String.valueOf(i));
        }
        //将所有marker点显示在屏幕范围内
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 200));//第二个参数为四周留空宽度
    }

    private AMap.OnMyLocationChangeListener mlocationChangeListener=new AMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {

           // ToastUtils.showLong(location.getLatitude()+";"+location.getLongitude()+";"+location.getAltitude());
            LogUtils.i("定位经纬度："+location.getLatitude()+";"+location.getLongitude()+";"+location.getAltitude());
        }
    };

    /**
     * 在地图上添加marker
     */
    private void addMarkersToMap(LatLng latlng, String title) {

        markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .position(latlng)
                .title(title)
                .draggable(true);

        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.drawable.location)));


        aMap.addMarker(markerOption);

        boundsBuilder.include(latlng);
    }


    // marker 对象被点击时回调的接口
// 返回 true 则表示接口已响应事件，否则返回false
    // 定义 Marker 点击事件监听
    AMap.OnMarkerClickListener markerClickListener = marker -> {
        String title = marker.getTitle();
        if (!StringUtils.isEmpty(title)) {

            String[] split = title.split(";");
            if (split.length == 2) {
                LogUtils.i(split[0] + "==" + split[1]);

            }

        }
        return true;
    };


    @Override
    public void initView() {
        super.initView();

    }


    @Override
    public void reLogin() {
        super.reLogin();
        aMap.clear(true);
    }

    @Override
    public void initData() {
        super.initData();
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        this.finish();
    }


    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图

        mMapView.onDestroy();
    }

    @Override
    public void onBackPressed() {
        //是否正在运动记录数据
        if (ISSTARTUP) {
            ToastUtils.showShort("退出请点击暂停按钮，在结束运动!");
            return;
        }

        super.onBackPressed();
    }





    private Bitmap drawCircle(int radius, int color) {

        Bitmap bitmap = Bitmap.createBitmap(radius * 2, radius * 2,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        RectF rectF = new RectF(0, 0, radius * 2, radius * 2);
        paint.setColor(color);
        canvas.drawArc(rectF, 0, 360, true, paint);
        return bitmap;
    }


}
